package Stack_Demo;

import java.util.Arrays;

public class MyStack implements IStack{
//    栈 的底层是 数组

    private int[] elem;
    private int usedSize;
    private static final int DEFAULT_CAPACITY=10;//默认容量

    public MyStack(){
        elem = new int[DEFAULT_CAPACITY];
    }
    @Override
    public void push(int x) {
//        进 栈
        if(full()){
            elem= Arrays.copyOf(elem,2*elem.length);
        }
        elem[usedSize++] = x;
        //等价于 else[usedSize] = x; usedSize++;
    }

    @Override
    public int pop() {
//        出 栈  出来就栈里面没了
        if (empty()){
            throw new EmptyException("栈, 空了");
        }
        int oldValue = elem[--usedSize];
        usedSize--;//相当与删除
        return oldValue;
    }

    @Override
    public int peek() {
//        获取栈顶元素 不删除
       if (empty()){
           throw new EmptyException("栈,空了!");
       }
       return elem[usedSize-1];
    }
    @Override
    public int size() {
        return usedSize;
    }

    @Override
    public boolean empty() {
        return usedSize == 0 ;
    }

    @Override
    public boolean full() {
        if(usedSize==elem.length){
            return true;
        }
        return false;
    }
}
